package com.merugu.school.beans;

import com.merugu.school.spring.StudentDao;
import net.sf.jasperreports.engine.*;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: raveendramerugu
 * Date: 12/28/10
 * Time: 1:36 PM
 * To change this template use File | Settings | File Templates.
 */

@ManagedBean(name = "studentBean")
@RequestScoped
public class StudentBean {

    private Long id;
    private String lastName;
    private String firstName;
    private String surName;
    private String gender;
    private String fatherName;
    private Date dateOfBirth;
    private Date dateOfJoin;
    private String motherName;

    public StudentBean() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getFatherName() {
        return fatherName;
    }


    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public Date getDateOfJoin() {
        return dateOfJoin;
    }

    public void setDateOfJoin(Date dateOfJoin) {
        this.dateOfJoin = dateOfJoin;
    }

    public String getMotherName() {
        return motherName;
    }

    public void setMotherName(String motherName) {
        this.motherName = motherName;
    }

    public String save() {
        StudentDao studentDao = StudentDao.getStudentDao();
        studentDao.saveStudent(this);

        return null;
    }

    public String receipt(){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        try {

        InputStream inputStream = StudentBean.class.getResourceAsStream("/receipt.jasper");
        JasperPrint jasperReport=JasperFillManager.fillReport(inputStream, new HashMap<String, Object>(), new JREmptyDataSource());
        facesContext.getExternalContext().addResponseHeader("Content-disposition","attachment;filename=receipt.pdf");
        OutputStream outputStream= facesContext.getExternalContext().getResponseOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperReport,outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
